import 'package:uikit/http/dao/login_dao.dart';

enum HttpMethod {
  GET,
  POST,
  DELETE,
}

///基础请求
abstract class BaseRequest {
  var pathParams;

  var useHttps = true;

  Map<String, String> params = {};

  Map<String, dynamic> header = {
    "course-flag": "fa",
    "auth-token": "MjAyMC0wNi0yMyAwMzoyNTowMQ==fa",
  };

  /// 请求方法
  HttpMethod get httpMethod;

  /// 请求api
  String get path;

  /// 是否需要登录
  bool get needLogin;

  /// 域名
  String authority() {
    return "api.devio.org";
  }

  String get url {
    Uri uri;
    var pathStr = path;
    //拼接path参数
    if (pathParams != null) {
      if (path.endsWith("/")) {
        pathStr = "$path$pathParams";
      } else {
        pathStr = "$path/$pathParams";
      }
    }
    //http和https切换
    if (useHttps) {
      uri = Uri.https(authority(), pathStr, params);
    } else {
      uri = Uri.http(authority(), pathStr, params);
    }
    if (needLogin) {
      //给需要登录的接口添加登录令牌
      addHeader(LoginDao.BOARDING_PASS, LoginDao.getBoardingPass());
    }

    print('网络请求url:${uri.toString()}');
    return uri.toString();
  }

  ///添加参数
  BaseRequest add(String k, dynamic v) {
    if (v != null) {
      params[k] = v.toString();
    }
    return this;
  }

  ///添加header
  BaseRequest addHeader(String k, Object v) {
    header[k] = v.toString();
    return this;
  }
}
